package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.constant.StrategyConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.PushMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Set;

/**
 * @author Zwnan
 */
@Service("limitFiveMinutes")
public class LimitFilterServiceImpl implements FilterChainService {

    @Autowired
    private CacheFeign cacheFeign;

    @Autowired
    private PushMsgUtil pushMsgUtil;


    @Override
    public boolean stratrgy(StandardSubmit submit) {
        Integer clientID = submit.getClientID();
        String destMobile = submit.getDestMobile();
        String key = CacheConstants.CACHE_PREFIX_SMS_LIMIT+clientID+":"+destMobile;
        long time = new Date().getTime();
        long five = time - 1000 * 60 * 5;
        Set<String> value = cacheFeign.zRangeByScore(key,five,time);
        if (value != null &&value.size()>0){
            pushMsgUtil.sendLog(submit, StrategyConstants.STRATEGY_ERROR_LIMIT);
            pushMsgUtil.pushReport(submit, StrategyConstants.STRATEGY_ERROR_LIMIT);
            return false;
        }else {
            return true;
        }

    }
}
